Imports System.Diagnostics.Process
Imports System.Threading
Public Class F_Recalculate
    Dim WithEvents SBO_Application As SAPbouiCOM.Application
    Dim Ocompany As SAPbobsCOM.Company
    Sub New(ByVal ocompany1 As SAPbobsCOM.Company, ByVal sbo_application1 As SAPbouiCOM.Application)
        SBO_Application = sbo_application1
        Ocompany = ocompany1
    End Sub
    Dim BPCodeRe As String = ""
    Dim oform1 As SAPbouiCOM.Form
    Private Sub SBO_Application_FormDataEvent(ByRef BusinessObjectInfo As SAPbouiCOM.BusinessObjectInfo, ByRef BubbleEvent As Boolean) Handles SBO_Application.FormDataEvent
        'Try
        '    If BusinessObjectInfo.FormTypeEx = 134 Then
        '        If BusinessObjectInfo.EventType = SAPbouiCOM.BoEventTypes.et_FORM_DATA_LOAD And BusinessObjectInfo.BeforeAction = True Then
        '            'Try
        '            '    oform1 = SBO_Application.Forms.GetFormByTypeAndCount(-134, 1)
        '            '    oform1.Close()
        '            'Catch ex As Exception

        '            'End Try

        '        End If
        '        If BusinessObjectInfo.EventType = SAPbouiCOM.BoEventTypes.et_FORM_DATA_LOAD And BusinessObjectInfo.ActionSuccess = True And BusinessObjectInfo.BeforeAction = False Then
        '            'If BusinessObjectInfo.EventType = SAPbouiCOM.BoEventTypes.et_FORM_DATA_LOAD And BusinessObjectInfo.ActionSuccess = True And BusinessObjectInfo.BeforeAction = False Then
        '            oForm = SBO_Application.Forms.Item(BusinessObjectInfo.FormUID)
        '            oEdit = oForm.Items.Item("5").Specific
        '            BPCodeRe = oEdit.String
        '            oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        '            oRecordSet2.DoQuery("SELECT T0.[CardCode] FROM OCRD T0 WHERE T0.[CardCode] ='" & BPCodeRe & "' and  T0.[CardType] ='C'")
        '            If oRecordSet2.RecordCount = 0 Then
        '                Exit Sub
        '            End If
        '            'Try
        '            '    oform1 = SBO_Application.Forms.GetFormByTypeAndCount(-134, 1)
        '            '    oform1.Close()
        '            'Catch ex As Exception

        '            'End Try
        '            'DisplayStatus(oForm, "Please Wait!", "")
        '            oForm.Freeze(True)
        '            DayPass()
        '            TransportBoarding()
        '            oForm.Refresh()
        '            ' SBO_Application.ActivateMenuItem("6913")
        '            Try
        '                oform1 = SBO_Application.Forms.GetFormByTypeAndCount(-134, 1)
        '                oform1.Freeze(True)
        '                oform1.Refresh()
        '                oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        '                oRecordSet2.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassUsed], T0.[U_AI_DayPassBal], T0.[U_AI_NoDayPassM], T0.[U_AI_DayPassUsedM], T0.[U_AI_DayPassBalM], T0.[U_AI_NoDayPassB], T0.[U_AI_DayPassUsedB], T0.[U_AI_DayPassBalB], T0.[U_AI_ACCDAYPASS], T0.[U_AI_TOTDP], T0.[U_AI_NoBoard], T0.[U_AI_BoardUsed], T0.[U_AI_BoardBal], T0.[U_AI_AccumBoard], T0.[U_AI_AccumBoardU], T0.[U_AI_NoTrans], T0.[U_AI_TransUsed], T0.[U_AI_TransBal], T0.[U_AI_LastTrans], T0.[U_AI_AccumTrans], T0.[U_AI_AccumTransU] FROM OCRD T0 WHERE T0.[CardCode] ='" & BPCodeRe & "'")
        '                ' T0.[U_AI_DayPassBalM], T0.[U_AI_NoDayPassB], T0.[U_AI_DayPassUsedB], T0.[U_AI_DayPassBalB], T0.[U_AI_ACCDAYPASS],
        '                ' T0.[U_AI_TOTDP],
        '                oEdit = oform1.Items.Item("U_AI_NoDayPass").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_NoDayPass").Value

        '                oEdit = oform1.Items.Item("U_AI_DayPassUsed").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_DayPassUsed").Value

        '                oEdit = oform1.Items.Item("U_AI_DayPassBal").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_DayPassBal").Value

        '                oEdit = oform1.Items.Item("U_AI_NoDayPassM").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_NoDayPassM").Value

        '                oEdit = oform1.Items.Item("U_AI_DayPassUsedM").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_DayPassUsedM").Value

        '                oEdit = oform1.Items.Item("U_AI_DayPassBalM").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_DayPassBalM").Value

        '                oEdit = oform1.Items.Item("U_AI_DayPassUsedB").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_DayPassUsedB").Value

        '                oEdit = oform1.Items.Item("U_AI_DayPassBalB").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_DayPassBalB").Value

        '                oEdit = oform1.Items.Item("U_AI_ACCDAYPASS").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_ACCDAYPASS").Value

        '                oEdit = oform1.Items.Item("U_AI_TOTDP").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_TOTDP").Value
        '                ' T0.[U_AI_NoBoard], T0.[U_AI_BoardUsed], T0.[U_AI_BoardBal], T0.[U_AI_AccumBoard], T0.[U_AI_AccumBoardU], T0.[U_AI_NoTrans], T0.[U_AI_TransUsed], T0.[U_AI_TransBal], T0.[U_AI_LastTrans], T0.[U_AI_AccumTrans], T0.[U_AI_AccumTransU] FROM OCRD T0 WHERE T0.[CardCode] ='" & BPCodeRe & "'")
        '                oEdit = oform1.Items.Item("U_AI_NoBoard").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_NoBoard").Value
        '                oEdit = oform1.Items.Item("U_AI_BoardBal").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_BoardBal").Value
        '                oEdit = oform1.Items.Item("U_AI_AccumBoard").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_AccumBoard").Value
        '                oEdit = oform1.Items.Item("U_AI_TOTDP").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_TOTDP").Value
        '                oEdit = oform1.Items.Item("U_AI_TOTDP").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_TOTDP").Value
        '                oEdit = oform1.Items.Item("U_AI_TOTDP").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_TOTDP").Value
        '                oEdit = oform1.Items.Item("U_AI_TOTDP").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_TOTDP").Value
        '                oEdit = oform1.Items.Item("U_AI_TOTDP").Specific
        '                oEdit.String = oRecordSet2.Fields.Item("U_AI_TOTDP").Value
        '                oform1.Freeze(False)
        '            Catch ex As Exception
        '                oform1.Freeze(False)
        '            End Try
        '            oForm.Freeze(False)
        '        End If
        '    End If
        'Catch ex As Exception
        '    oForm.Freeze(False)
        '    SBO_Application.ActivateMenuItem("6913")
        '    '            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
        'End Try
    End Sub
    Public Sub DayPass()
        Try
            OpeningBal_DayPass()
            DayPass_Invoice_loop()
            DayPass_CN_loop()
            DayPass_Used()
        Catch ex As Exception
            'Functions.WriteLog(ex.Message)
        End Try

    End Sub
    Public Sub TransportBoarding()
        Try
            '-------Boarding---------------
            OpeningBalance_Bor()
            Boarding_Invoice()
            Boarding_CN()
            Boarding_Attendance()


        Catch ex As Exception
            '' Functions.WriteLog(ex.Message)

        End Try
        Try
            '-------Transport---------------
            OpeningBalance_Trs()
            Transport_Invoice()
            Transport_CN()
            Transport_Attendance()

        Catch ex As Exception
            '' Functions.WriteLog(ex.Message)

        End Try

    End Sub
#Region "DP"
    Public Sub OpeningBal_DayPass()
        Try
            Try
                'Dim Filenum As Integer = FreeFile()
                'FileOpen(Filenum, "C:\Log\ErrorLog.txt", OpenMode.Output)
                'FileClose()
            Catch ex As Exception

            End Try
            Dim PrimKey As String = BPCodeRe
            'If TextBox1.Text <> "" Then
            '    PrimKey = TextBox1.Text
            'End If

            'Dim file_CustRelation As System.IO.StreamWriter = New System.IO.StreamWriter("C:\Log\ErrorLog" & TDT & ".txt", True)
            'Dim file As System.IO.StreamWriter = New System.IO.StreamWriter("C:\Log\ErrorLog.txt", True)
            'Dim csvFileName As String = "K9DP1.csv"
            'Dim connString As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" _
            '       & "C:\k9\" & ";Extended Properties=""Text;HDR=No;FMT=Delimited"""
            'Dim conn As New Odbc.OdbcConnection(connString)
            'Dim da2 As New Odbc.OdbcDataAdapter("SELECT * FROM [" & csvFileName & "]", conn)
            'Dim DtSet_Master As System.Data.DataSet
            'DtSet_Master = New System.Data.DataSet
            'da2.Fill(DtSet_Master)
            'da2.Fill(DtSet_Master)
            Dim oRecordsetOB As SAPbobsCOM.Recordset
            oRecordsetOB = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordsetOB.DoQuery("SELECT T0.[U_Petcode], T0.[U_CardCode], T0.[U_DP], T0.[U_TotalDP] FROM [dbo].[@OPENINGBALANCE]  T0 where T0.[U_CardCode] like '" & PrimKey & "' ")

            Dim rowcount_Master As Integer = oRecordsetOB.RecordCount 'DtSet_Master.Tables(0).Rows.Count
            '------------
            Dim i As Integer
            Dim BPCode As String = ""
            Dim PetCode As String = ""
            Dim PetSize As String = ""
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet As SAPbobsCOM.Recordset
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            Dim OldNoBor As Integer = 0
            Dim OldNoBorUsed As Integer = 0
            Dim NewNoBorBal As Integer = 0
            Dim NewNoBor As Integer = 0

            Dim OldNoTrs As Integer = 0
            Dim OldNoTrsUsed As Integer = 0
            Dim NewNoTrsBal As Integer = 0
            Dim NewNoTrs As Integer = 0

            Dim OldNoDPSmall As Integer = 0
            Dim OldNoDPSmallUsed As Integer = 0
            Dim NewNoDPSmallBal As Integer = 0
            Dim NewNoDPSmall As Integer = 0

            Dim OldNoDPMedium As Integer = 0
            Dim OldNoDPMediumUsed As Integer = 0
            Dim NewNoDPMediumBal As Integer = 0
            Dim NewNoDPMedium As Integer = 0

            Dim OldNoDPLarge As Integer = 0
            Dim OldNoDPLargeUsed As Integer = 0
            Dim NewNoDPLargeBal As Integer = 0
            Dim NewNoDPLarge As Integer = 0

            Dim str As String = ""
            Dim k As Integer = 0
            'Try


            '    For i = 0 To rowcount_Master - 1
            '  BPCode = oRecordsetOB.Fields.Item(1).Value 'DtSet_Master.Tables(0).Rows(i).Item(3).ToString()
            NewNoDPMedium = 0
            NewNoDPMediumBal = 0
            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            str = "update ocrd set U_AI_ACCDAYPASS='" & NewNoDPMedium & "',U_AI_NoDayPass='" & NewNoDPMedium & "',[U_AI_DayPassBal]='" & NewNoDPMediumBal & "',U_AI_NoDayPassM='" & NewNoDPMedium & "',[U_AI_DayPassBalM]='" & NewNoDPMediumBal & "',U_AI_NoDayPassB='" & NewNoDPMedium & "',[U_AI_DayPassBalB]='" & NewNoDPMediumBal & "' where Cardcode like '" & PrimKey & "'"
            oRecordSet3.DoQuery(str)
            '        Next
            'Catch ex As Exception
            '    'MsgBox(ex.Message)
            'End Try
            For i = 0 To rowcount_Master - 1

                Try

                    'If i = 32 Then
                    '    'MsgBox("Hi")
                    'End If
                    'k = k + i
                    BPCode = oRecordsetOB.Fields.Item(1).Value 'DtSet_Master.Tables(0).Rows(i).Item(3).ToString()
                    If BPCode <> "" Or BPCode <> 0 Then
                        PetCode = oRecordsetOB.Fields.Item(0).Value 'DtSet_Master.Tables(0).Rows(i).Item(0).ToString.Trim
                        oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        str = "SELECT T0.[U_AI_Size] FROM [dbo].[@AI_PETMASTER]  T0 WHERE T0.[U_PetCode] ='" & PetCode & "'"
                        oRecordSet.DoQuery(str)
                        PetSize = oRecordSet.Fields.Item(0).Value
                        oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        str = "SELECT isnull(T0.[U_AI_NoBoard],0), isnull(T0.[U_AI_BoardUsed],0),isnull(T0.[U_AI_NoTrans],0),isnull(T0.[U_AI_TransUsed],0),isnull(T0.[U_AI_NoDayPass],0),isnull(T0.[U_AI_DayPassUsed],0),isnull(T0.[U_AI_NoDayPassM],0),isnull(T0.[U_AI_DayPassUsedM],0),isnull(T0.[U_AI_NoDayPassB],0),isnull(T0.[U_AI_DayPassUsedB],0) FROM OCRD T0 WHERE T0.[CardCode] ='" & BPCode & "'"
                        oRecordSet2.DoQuery(str)

                        If PetSize = "Small" Then
                            OldNoDPSmall = oRecordSet2.Fields.Item(4).Value
                            OldNoDPSmallUsed = oRecordSet2.Fields.Item(5).Value
                            NewNoDPSmall = oRecordsetOB.Fields.Item(2).Value + OldNoDPSmall
                            NewNoDPSmallBal = NewNoDPSmall - OldNoDPSmallUsed
                            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                            str = "update ocrd set U_AI_NoDayPass='" & NewNoDPSmall & "',[U_AI_DayPassBal]='" & NewNoDPSmallBal & "' where cardcode='" & BPCode & "'"
                            oRecordSet3.DoQuery(str)

                        ElseIf PetSize = "Medium" Then
                            OldNoDPMedium = oRecordSet2.Fields.Item(6).Value
                            OldNoDPMediumUsed = oRecordSet2.Fields.Item(7).Value
                            NewNoDPMedium = oRecordsetOB.Fields.Item(2).Value + OldNoDPMedium
                            NewNoDPMediumBal = NewNoDPMedium - OldNoDPMediumUsed
                            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                            str = "update ocrd set U_AI_NoDayPassM='" & NewNoDPMedium & "',[U_AI_DayPassBalM]='" & NewNoDPMediumBal & "' where cardcode='" & BPCode & "'"
                            oRecordSet3.DoQuery(str)



                        ElseIf PetSize = "Large" Then
                            OldNoDPLarge = oRecordSet2.Fields.Item(8).Value
                            OldNoDPLargeUsed = oRecordSet2.Fields.Item(9).Value
                            NewNoDPLarge = oRecordsetOB.Fields.Item(2).Value + OldNoDPLarge
                            NewNoDPLargeBal = NewNoDPLarge - OldNoDPLargeUsed
                            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                            str = "update ocrd set U_AI_NoDayPassB='" & NewNoDPLarge & "',[U_AI_DayPassBalB]='" & NewNoDPLargeBal & "' where cardcode='" & BPCode & "'"
                            oRecordSet3.DoQuery(str)
                        End If
                    End If
                Catch ex As Exception
                    '' Functions.WriteLog(ex.Message)
                    'MsgBox(ex.Message & i)
                End Try
                Try
                    oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    str = "SELECT (isnull(T0.[U_AI_NoDayPass],0)+isnull(T0.[U_AI_NoDayPassM],0)+isnull(T0.[U_AI_NoDayPassB],0)) as 'Tot' FROM OCRD T0 where T0.[CardCode]='" & BPCode & "'"
                    oRecordSet3.DoQuery(str)
                    Dim acc As Integer = oRecordSet3.Fields.Item(0).Value
                    oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    str = "update ocrd set U_AI_ACCDAYPASS='" & acc & "' where cardcode='" & BPCode & "'"
                    oRecordSet3.DoQuery(str)
                Catch ex As Exception
                    ' Functions.WriteLog(ex.Message)
                    'MsgBox(ex.Message & i)
                End Try
                oRecordsetOB.MoveNext()
            Next
            oRecordsetOB.MoveFirst()
            For i = 0 To rowcount_Master - 1
                BPCode = oRecordsetOB.Fields.Item(1).Value 'DtSet_Master.Tables(0).Rows(i).Item(3).ToString()
                Try
                    oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    str = "SELECT (isnull(T0.[U_AI_ACCDAYPASS],0)) as 'Tot' FROM OCRD T0 where T0.[CardCode]='" & BPCode & "'"
                    oRecordSet3.DoQuery(str)
                    Dim acc As Integer = 0
                    If oRecordSet3.Fields.Item(0).Value > 0 Then
                        acc = oRecordSet3.Fields.Item(0).Value + oRecordsetOB.Fields.Item(3).Value 'DtSet_Master.Tables(0).Rows(i).Item(4)
                    Else
                        acc = oRecordsetOB.Fields.Item(3).Value 'DtSet_Master.Tables(0).Rows(i).Item(4)
                    End If

                    oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    str = "update ocrd set U_AI_ACCDAYPASS='" & acc & "' where cardcode='" & BPCode & "'"
                    oRecordSet3.DoQuery(str)
                Catch ex As Exception
                    ' Functions.WriteLog(ex.Message)
                    'MsgBox(ex.Message & i)
                End Try
                oRecordsetOB.MoveNext()
            Next

            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            str = "update OCRD set U_AI_DayPassBal=U_AI_NoDayPass-U_AI_DayPassUsed"
            oRecordSet3.DoQuery(str)
            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            str = "update OCRD set U_AI_DayPassBalM=U_AI_NoDayPassM-U_AI_DayPassUsedM"
            oRecordSet3.DoQuery(str)
            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            str = "update OCRD set U_AI_DayPassBalB=U_AI_NoDayPassB-U_AI_DayPassUsedB"
            oRecordSet3.DoQuery(str)
            'update OCRD set U_AI_DayPassBal=U_AI_NoDayPass-U_AI_DayPassUsed
            'update OCRD set U_AI_DayPassBalM=U_AI_NoDayPassM-U_AI_DayPassUsedM
            'update OCRD set U_AI_DayPassBalB=U_AI_NoDayPassB-U_AI_DayPassUsedB
            ''MsgBox("Complected")
        Catch ex As Exception
            ' Functions.WriteLog(ex.Message)
            'MsgBox(ex.Message)
        End Try
    End Sub
    Public Function PSize(ByVal PetCode As String, ByVal Ocompany As SAPbobsCOM.Company) As String
        Dim x As String = ""
        Dim oRecordSet_PZ As SAPbobsCOM.Recordset
        oRecordSet_PZ = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        Dim Str As String = "SELECT T0.[U_AI_Size] FROM [dbo].[@AI_PETMASTER]  T0 WHERE T0.[U_PetCode] ='" & PetCode & "'"
        oRecordSet_PZ.DoQuery("SELECT T0.[U_AI_Size] FROM [dbo].[@AI_PETMASTER]  T0 WHERE T0.[U_PetCode] ='" & PetCode & "'")
        x = oRecordSet_PZ.Fields.Item(0).Value
        Return x
    End Function
    Public Sub DayPass_Invoice_loop()
        Dim PrimKey As String = BPCodeRe
        'If TextBox1.Text <> "" Then
        '    PrimKey = TextBox1.Text
        'End If
        Dim oRecordSetCLop As SAPbobsCOM.Recordset
        oRecordSetCLop = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        oRecordSetCLop.DoQuery("select DocEntry from OINV where CardCode like '" & PrimKey & "'")
        Dim M As Integer = 0
        Dim oRecordSet5 As SAPbobsCOM.Recordset
        oRecordSet5 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        oRecordSet5.DoQuery("update ocrd set U_AI_DayPassUsed=0,U_AI_DayPassUsedM=0,U_AI_DayPassUsedB=0 where CardCode like '" & PrimKey & "'")

        oRecordSetCLop.MoveFirst()
        For M = 0 To oRecordSetCLop.RecordCount - 1

            DayPass_Invoice(oRecordSetCLop.Fields.Item(0).Value.ToString)
            oRecordSetCLop.MoveNext()
        Next
    End Sub
    Public Sub DayPass_Invoice(ByVal docnum As String)
        Try
            Dim oRecordSet As SAPbobsCOM.Recordset
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            Dim oRecordSet4 As SAPbobsCOM.Recordset
            Dim oRecordSet5 As SAPbobsCOM.Recordset
            oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)

            'oRecordSet51.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[Price],T1.[U_AI_PetCode] FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='102')")

            Dim PetSize As String = ""
            oRecordSet.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[U_AI_PetCode] FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='104') and T0.DocEntry ='" & docnum & "'")

            Dim oRecordSet6 As SAPbobsCOM.Recordset
            oRecordSet6 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet6.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[U_AI_PetCode],T1.[U_AI_PackageType] FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='102') and T0.DocEntry ='" & docnum & "'")
            If oRecordSet.RecordCount = 0 And oRecordSet6.RecordCount = 0 Then
                Exit Sub
            End If


            Dim i As Integer
            Dim Qty As Integer = 0
            Dim Notrans As Integer = 0
            Dim AccTrans As Integer = 0
            Dim NewNotrans As Integer = 0
            Dim NewAccTrans As Integer = 0
            Dim SmallCount As Integer = 0
            Dim MediumCount As Integer = 0
            Dim LargeCount As Integer = 0
            oRecordSet.MoveFirst()
            Dim Edt As String = Format(Now.Date.AddMonths(6), "yyyyMMdd")
            '[U_AI_ExpDate]
            Dim Small As Boolean = False
            Dim Medium As Boolean = False
            Dim Large As Boolean = False
            Dim Qty1 As Integer = 0
            For i = 1 To oRecordSet.RecordCount
                Dim CardCode As String = oRecordSet.Fields.Item(0).Value
                PetSize = PSize(oRecordSet.Fields.Item(2).Value, Ocompany)
                Qty1 = Qty1 + oRecordSet.Fields.Item(1).Value
                If PetSize = "Small" Then
                    Small = True
                    SmallCount = SmallCount + 1
                ElseIf PetSize = "Medium" Then
                    Medium = True
                    MediumCount = MediumCount + 1
                ElseIf PetSize = "Large" Then
                    Large = True
                    LargeCount = LargeCount + 1
                End If
                oRecordSet.MoveNext()
            Next
            oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
            Dim j As Integer
            oRecordSet3.MoveFirst()
            Dim Bonus1 As Integer = 0
            Dim Bought1 As Integer = 0
            Dim BonusPoint1 As Integer = 0
            For j = 1 To oRecordSet3.RecordCount
                Bought1 = oRecordSet3.Fields.Item(0).Value
                Bonus1 = oRecordSet3.Fields.Item(1).Value
                If Qty1 >= Bought1 Then
                    BonusPoint1 = Bonus1
                End If
                oRecordSet3.MoveNext()
            Next
            Dim SmallBP As Integer = 0
            Dim MediumBP As Integer = 0
            Dim LargeBP As Integer = 0
            Dim BonusPoint2 As Integer = 0
            BonusPoint2 = BonusPoint1
            For j = 1 To BonusPoint1
                If Small = True Then
                    SmallBP = SmallBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
                If Medium = True Then
                    MediumBP = MediumBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
                If Large = True Then
                    LargeBP = LargeBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
            Next

            oRecordSet.MoveFirst()
            For i = 1 To oRecordSet.RecordCount
                Dim CardCode As String = oRecordSet.Fields.Item(0).Value

                PetSize = PSize(oRecordSet.Fields.Item(2).Value, Ocompany)
                If PetSize = "Small" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + (SmallBP)
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans + (SmallBP)
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    If SmallBP <> 0 Then
                        SmallBP = 0
                    End If
                    'update ocrd set groupcode=" & GroupCode & ",
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                ElseIf PetSize = "Medium" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassM], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedM] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    'Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + (MediumBP)
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans + (MediumBP)
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    If MediumBP <> 0 Then
                        MediumBP = 0
                    End If
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "', U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "', U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                ElseIf PetSize = "Large" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassB], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedB] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    'Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + (LargeBP)
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans + (LargeBP)
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If LargeBP <> 0 Then
                        LargeBP = 0
                    End If
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "' , groupcode=" & GroupCode & ",U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                End If

                oRecordSet.MoveNext()
            Next
            '---annual pack
            Edt = Format(Now.Date.AddMonths(12), "yyyyMMdd")
            For i = 1 To oRecordSet6.RecordCount
                Dim CardCode As String = oRecordSet6.Fields.Item(0).Value

                PetSize = PSize(oRecordSet6.Fields.Item(2).Value, Ocompany)
                Dim PackType = oRecordSet6.Fields.Item(3).Value
                Dim oRecordSet7 As SAPbobsCOM.Recordset
                oRecordSet7 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet7.DoQuery("SELECT T0.[U_AI_DaysPerWeek] FROM [dbo].[@AI_CONTRACTTYPE]  T0 WHERE T0.[Code] ='" & PackType & "'")
                If PetSize = "Small" Then
                    Qty = oRecordSet6.Fields.Item(1).Value
                    Qty = Qty * oRecordSet7.Fields.Item(0).Value * 4
                    'oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + BonusPoint
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans + BonusPoint
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewAccTrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    'update ocrd set groupcode=" & GroupCode & ",
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                ElseIf PetSize = "Medium" Then

                    Qty = oRecordSet6.Fields.Item(1).Value
                    Qty = Qty * oRecordSet7.Fields.Item(0).Value * 4
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassM], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedM] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    'oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + BonusPoint
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans + BonusPoint
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewAccTrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "',U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                ElseIf PetSize = "Large" Then
                    Qty = oRecordSet6.Fields.Item(1).Value
                    Qty = Qty * oRecordSet7.Fields.Item(0).Value * 4
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassB], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedB] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Qty + Notrans + BonusPoint
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = Qty + AccTrans + BonusPoint
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewAccTrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "' , groupcode=" & GroupCode & ",U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                End If

                oRecordSet6.MoveNext()
            Next
            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            oRecordSet6 = Nothing
            GC.Collect()
        Catch ex As Exception
            ' Functions.WriteLog(ex.Message)
        End Try
    End Sub
    Public Sub DayPass_CN_loop()
        Dim PrimKey As String = BPCodeRe
        'If TextBox1.Text <> "" Then
        '    PrimKey = TextBox1.Text
        'End If
        Dim oRecordSetCLop As SAPbobsCOM.Recordset
        oRecordSetCLop = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        oRecordSetCLop.DoQuery("select DocEntry from ORIN where cardCode like '" & PrimKey & "'")
        Dim M As Integer = 0
        oRecordSetCLop.MoveFirst()
        For M = 0 To oRecordSetCLop.RecordCount - 1
            DayPass_CN(oRecordSetCLop.Fields.Item(0).Value.ToString)
            oRecordSetCLop.MoveNext()
        Next
    End Sub
    Public Sub DayPass_Used()
        Try

            Dim PrimKey As String = BPCodeRe
            'If TextBox1.Text <> "" Then
            '    PrimKey = TextBox1.Text
            'End If

            'SELECT T0.[U_AI_PetCode],Count(*) as total FROM [dbo].[@AI_ATTENDANCE]  T0 WHERE T0.[U_AI_PetCode]  <> '' GROUP BY T0.[U_AI_PetCode]
            Dim oRecordSetPet As SAPbobsCOM.Recordset
            Dim oRecordSet As SAPbobsCOM.Recordset
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            'Dim oRecordSet2 As SAPbobsCOM.Recordset
            oRecordSetPet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSetPet.DoQuery("SELECT T0.[U_AI_PetCode],Count(*) as total FROM [dbo].[@AI_ATTENDANCE]  T0 WHERE T0.[U_AI_PetCode]  <> '' and isnull( T0.[U_Ignore],'')  <> 'Y' GROUP BY T0.[U_AI_PetCode]")
            Dim i As Integer = 0
            Dim BPCode As String = ""
            Dim PetSize As String = ""
            Dim PetCode As String = ""
            Dim Count As Integer = 0
            oRecordSetPet.MoveFirst()
            For i = 0 To oRecordSetPet.RecordCount - 1
                PetCode = oRecordSetPet.Fields.Item(0).Value.ToString
                Count = oRecordSetPet.Fields.Item(1).Value.ToString
                BPCode = OwnerCode(PetCode, Ocompany)
                If PrimKey = "%" Or BPCode = PrimKey Then
                    PetSize = PSize(PetCode, Ocompany)
                    '------------------
                    If PetSize = "Small" Then

                        Dim NoDaypass As Integer = 0
                        Dim DaypassUsed As Integer = 0
                        Dim AccNoDaypass As Integer = 0
                        Dim AccDaypassUsed As Integer = 0
                        Dim newDayPassUsed As Integer = 0
                        Dim NewDayPassBal As Integer = 0
                        Dim NewAccDaypassUsed As Integer = 0
                        oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassUsed], T0.[U_AI_DayPassBal], T0.[U_AI_LastVisit], T0.[U_AI_ACCDAYPASS], T0.[U_AI_TOTDP] FROM OCRD T0 WHERE T0.[CardCode]  ='" & BPCode & "'")
                        NoDaypass = oRecordSet.Fields.Item(0).Value
                        DaypassUsed = oRecordSet.Fields.Item(1).Value
                        AccNoDaypass = oRecordSet.Fields.Item(4).Value
                        AccDaypassUsed = oRecordSet.Fields.Item(5).Value
                        newDayPassUsed = DaypassUsed + Count
                        NewDayPassBal = NoDaypass - newDayPassUsed
                        NewAccDaypassUsed = AccDaypassUsed + Count
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("UPDATE OCRD SET U_AI_DayPassBal=" & NewDayPassBal & ",U_AI_DayPassUsed=" & newDayPassUsed & ",U_AI_TOTDP=" & NewAccDaypassUsed & " WHERE CardCode='" & BPCode & "' ")

                        '===========

                    ElseIf PetSize = "Medium" Then
                        Dim NoDaypass As Integer = 0
                        Dim DaypassUsed As Integer = 0
                        Dim AccNoDaypass As Integer = 0
                        Dim AccDaypassUsed As Integer = 0
                        Dim newDayPassUsed As Integer = 0
                        Dim NewDayPassBal As Integer = 0
                        Dim NewAccDaypassUsed As Integer = 0
                        oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet.DoQuery("SELECT T0.[U_AI_NoDayPassM], T0.[U_AI_DayPassUsedM], T0.[U_AI_DayPassBalM], T0.[U_AI_LastVisit], T0.[U_AI_ACCDAYPASS], T0.[U_AI_TOTDP] FROM OCRD T0 WHERE T0.[CardCode]  ='" & BPCode & "'")
                        NoDaypass = oRecordSet.Fields.Item(0).Value
                        DaypassUsed = oRecordSet.Fields.Item(1).Value
                        AccNoDaypass = oRecordSet.Fields.Item(4).Value
                        AccDaypassUsed = oRecordSet.Fields.Item(5).Value
                        newDayPassUsed = DaypassUsed + Count
                        NewDayPassBal = NoDaypass - newDayPassUsed
                        NewAccDaypassUsed = AccDaypassUsed + Count
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM=" & NewDayPassBal & ",U_AI_DayPassUsedM=" & newDayPassUsed & ",U_AI_TOTDP=" & NewAccDaypassUsed & " WHERE CardCode='" & BPCode & "' ")
                        '============

                        '===========
                    ElseIf PetSize = "Large" Then
                        Dim NoDaypass As Integer = 0
                        Dim DaypassUsed As Integer = 0
                        Dim AccNoDaypass As Integer = 0
                        Dim AccDaypassUsed As Integer = 0
                        Dim newDayPassUsed As Integer = 0
                        Dim NewDayPassBal As Integer = 0
                        Dim NewAccDaypassUsed As Integer = 0
                        oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet.DoQuery("SELECT T0.[U_AI_NoDayPassB], T0.[U_AI_DayPassUsedB], T0.[U_AI_DayPassBalB], T0.[U_AI_LastVisit], T0.[U_AI_ACCDAYPASS], T0.[U_AI_TOTDP] FROM OCRD T0 WHERE T0.[CardCode]  ='" & BPCode & "'")
                        NoDaypass = oRecordSet.Fields.Item(0).Value
                        DaypassUsed = oRecordSet.Fields.Item(1).Value
                        AccNoDaypass = oRecordSet.Fields.Item(4).Value
                        AccDaypassUsed = oRecordSet.Fields.Item(5).Value
                        newDayPassUsed = DaypassUsed + Count
                        NewDayPassBal = NoDaypass - newDayPassUsed
                        NewAccDaypassUsed = AccDaypassUsed + Count
                        oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                        oRecordSet1.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB=" & NewDayPassBal & ",U_AI_DayPassUsedB=" & newDayPassUsed & ",U_AI_TOTDP=" & NewAccDaypassUsed & " WHERE CardCode='" & BPCode & "' ")
                        '============

                    End If
                End If
                '------------------------
                oRecordSetPet.MoveNext()
            Next
            oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1.DoQuery("update OCRD set U_AI_TOTDP=U_AI_DayPassUsed+U_AI_DayPassUsedM+U_AI_DayPassUsedB")
        Catch ex As Exception
            ' Functions.WriteLog(ex.Message)
            'MsgBox(ex.Message)
        End Try

    End Sub
    Public Sub DayPass_CN(ByVal DocNum As String)
        Try

            Dim oRecordSet As SAPbobsCOM.Recordset
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            Dim oRecordSet4 As SAPbobsCOM.Recordset
            Dim oRecordSet5 As SAPbobsCOM.Recordset
            'SBO_Application.StatusBar.SetText("Hi............", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
            oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet5 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            Dim oRecordSet51 As SAPbobsCOM.Recordset
            oRecordSet51 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet5.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[Price],T1.[U_AI_PetCode] FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='104') and T0.DocEntry ='" & DocNum & "'")
            oRecordSet51.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[Price],T1.[U_AI_PetCode] FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='102') and T0.DocEntry ='" & DocNum & "'")

            Dim PetSize As String = ""
            oRecordSet.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[U_AI_PetCode] FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='104') and T0.DocEntry ='" & DocNum & "'")

            Dim oRecordSet6 As SAPbobsCOM.Recordset
            oRecordSet6 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet6.DoQuery("SELECT T0.[CardCode], T1.[Quantity],T1.[U_AI_PetCode],T1.[U_AI_PackageType] FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE ((select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='102') and T0.DocEntry ='" & DocNum & "'")
            If oRecordSet.RecordCount = 0 And oRecordSet6.RecordCount = 0 Then
                Exit Sub
            End If

            'If oRecordSet51.RecordCount > 0 Then
            '    PetSize = PSize(oRecordSet51.Fields.Item(3).Value,  oCompany)
            '    Dim CNo As String = oRecordSet51.Fields.Item(0).Value.ToString
            '    Dim upp As String = oRecordSet51.Fields.Item("Price").Value
            '    Dim dpbou As String = oRecordSet51.Fields.Item(1).Value
            '    Invoice_DayPass(DocNum, CNo, upp, PetSize, dpbou)
            '    ' Invoice_AnnualPackage_update(DocNum, CNo, upp, PetSize, dpbou)
            'End If

            'If oRecordSet5.RecordCount > 0 Then
            '    PetSize = PSize(oRecordSet5.Fields.Item(3).Value,  oCompany)
            '    Dim CNo As String = oRecordSet5.Fields.Item(0).Value.ToString
            '    Dim upp As String = oRecordSet5.Fields.Item("Price").Value
            '    Dim dpbou As String = oRecordSet5.Fields.Item(1).Value
            '    Invoice_DayPass(DocNum, CNo, upp, PetSize, dpbou)
            '    ' Invoice_AnnualPackage_update(DocNum, CNo, upp, PetSize, dpbou)
            'End If


            Dim i As Integer
            Dim Qty As Integer = 0
            Dim Notrans As Integer = 0
            Dim AccTrans As Integer = 0
            Dim NewNotrans As Integer = 0
            Dim NewAccTrans As Integer = 0
            Dim SmallCount As Integer = 0
            Dim MediumCount As Integer = 0
            Dim LargeCount As Integer = 0
            oRecordSet.MoveFirst()
            Dim Edt As String = Format(Now.Date.AddMonths(6), "yyyyMMdd")
            '[U_AI_ExpDate]
            Dim Small As Boolean = False
            Dim Medium As Boolean = False
            Dim Large As Boolean = False
            Dim Qty1 As Integer = 0
            For i = 1 To oRecordSet.RecordCount
                Dim CardCode As String = oRecordSet.Fields.Item(0).Value
                PetSize = PSize(oRecordSet.Fields.Item(2).Value, Ocompany)
                Qty1 = Qty1 + oRecordSet.Fields.Item(1).Value
                If PetSize = "Small" Then
                    Small = True
                    SmallCount = SmallCount + 1
                ElseIf PetSize = "Medium" Then
                    Medium = True
                    MediumCount = MediumCount + 1
                ElseIf PetSize = "Large" Then
                    Large = True
                    LargeCount = LargeCount + 1
                End If
                oRecordSet.MoveNext()
            Next
            oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
            Dim j As Integer
            oRecordSet3.MoveFirst()
            Dim Bonus1 As Integer = 0
            Dim Bought1 As Integer = 0
            Dim BonusPoint1 As Integer = 0
            For j = 1 To oRecordSet3.RecordCount
                Bought1 = oRecordSet3.Fields.Item(0).Value
                Bonus1 = oRecordSet3.Fields.Item(1).Value
                If Qty1 >= Bought1 Then
                    BonusPoint1 = Bonus1
                End If
                oRecordSet3.MoveNext()
            Next
            Dim SmallBP As Integer = 0
            Dim MediumBP As Integer = 0
            Dim LargeBP As Integer = 0
            Dim BonusPoint2 As Integer = 0
            BonusPoint2 = BonusPoint1
            For j = 1 To BonusPoint1
                If Small = True Then
                    SmallBP = SmallBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
                If Medium = True Then
                    MediumBP = MediumBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
                If Large = True Then
                    LargeBP = LargeBP + 1
                    BonusPoint2 = BonusPoint2 - 1
                    If BonusPoint2 = 0 Then
                        Exit For
                    End If
                End If
            Next

            oRecordSet.MoveFirst()
            For i = 1 To oRecordSet.RecordCount
                Dim CardCode As String = oRecordSet.Fields.Item(0).Value

                PetSize = PSize(oRecordSet.Fields.Item(2).Value, Ocompany)
                If PetSize = "Small" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = (Notrans - (SmallBP)) - Qty
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = (AccTrans - (SmallBP)) - Qty
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused
                    If SmallBP <> 0 Then
                        SmallBP = 0
                    End If
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    'update ocrd set groupcode=" & GroupCode & ",
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                ElseIf PetSize = "Medium" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassM], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedM] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    'Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = (Notrans - (MediumBP)) - Qty
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = (AccTrans - (MediumBP)) - Qty
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused
                    If MediumBP <> 0 Then
                        MediumBP = 0
                    End If
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "', U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "', U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                ElseIf PetSize = "Large" Then
                    Qty = oRecordSet.Fields.Item(1).Value
                    ' oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassB], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedB] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    'Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    'oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = (Notrans - (LargeBP)) - Qty
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = (AccTrans - (LargeBP)) - Qty
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewNotrans - OldDPused
                    If LargeBP <> 0 Then
                        LargeBP = 0
                    End If
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "' , groupcode=" & GroupCode & ",U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                End If

                oRecordSet.MoveNext()
            Next
            '---annual pack
            Edt = Format(Now.Date.AddMonths(12), "yyyyMMdd")
            For i = 1 To oRecordSet6.RecordCount
                Dim CardCode As String = oRecordSet6.Fields.Item(0).Value

                PetSize = PSize(oRecordSet6.Fields.Item(2).Value, Ocompany)
                Dim PackType = oRecordSet6.Fields.Item(3).Value
                Dim oRecordSet7 As SAPbobsCOM.Recordset
                oRecordSet7 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet7.DoQuery("SELECT T0.[U_AI_DaysPerWeek] FROM [dbo].[@AI_CONTRACTTYPE]  T0 WHERE T0.[Code] ='" & PackType & "'")
                If PetSize = "Small" Then
                    Qty = oRecordSet6.Fields.Item(1).Value
                    Qty = Qty * oRecordSet7.Fields.Item(0).Value * 4
                    'oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Notrans + BonusPoint - Qty
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = AccTrans + BonusPoint - Qty
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewAccTrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    'update ocrd set groupcode=" & GroupCode & ",
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBal='" & NewBal & "',U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPass=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                ElseIf PetSize = "Medium" Then

                    Qty = oRecordSet6.Fields.Item(1).Value
                    Qty = Qty * oRecordSet7.Fields.Item(0).Value * 4
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassM], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedM] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    'oRecordSet3.DoQuery("SELECT T0.[U_AI_NoDayPass], T0.[U_AI_DayPassBonus] FROM [dbo].[@AI_BONUSMATRIX]  T0")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Notrans + BonusPoint - Qty
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = AccTrans + BonusPoint - Qty
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewAccTrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalM='" & NewBal & "',U_AI_ExpDate='" & Edt & "',groupcode=" & GroupCode & ",U_AI_NoDayPassM=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                ElseIf PetSize = "Large" Then
                    Qty = oRecordSet6.Fields.Item(1).Value
                    Qty = Qty * oRecordSet7.Fields.Item(0).Value * 4
                    oRecordSet1.DoQuery("SELECT T0.[U_AI_NoDayPassB], T0.[U_AI_ACCDAYPASS],T0.[U_AI_DayPassUsedB] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                    ' Dim j As Integer
                    Dim Bonus As Integer = 0
                    Dim Bought As Integer = 0
                    Dim BonusPoint As Integer = 0
                    oRecordSet3.MoveFirst()
                    'For j = 1 To oRecordSet3.RecordCount
                    '    Bought = oRecordSet3.Fields.Item(0).Value
                    '    Bonus = oRecordSet3.Fields.Item(1).Value
                    '    If Qty >= Bought Then
                    '        BonusPoint = Bonus
                    '    End If
                    '    oRecordSet3.MoveNext()
                    'Next
                    Notrans = oRecordSet1.Fields.Item(0).Value
                    NewNotrans = Notrans + BonusPoint - Qty
                    AccTrans = oRecordSet1.Fields.Item(1).Value
                    NewAccTrans = AccTrans + BonusPoint - Qty
                    Dim OldDPused As Integer = 0
                    Dim NewBal As Integer = 0
                    OldDPused = oRecordSet1.Fields.Item(2).Value
                    NewBal = NewAccTrans - OldDPused
                    Dim GroupCode As Integer = 0
                    If NewAccTrans >= 0 And NewAccTrans <= 19 Then
                        GroupCode = 105
                    ElseIf NewAccTrans >= 20 And NewAccTrans <= 39 Then
                        GroupCode = 100
                    ElseIf NewAccTrans >= 40 And NewAccTrans <= 79 Then
                        GroupCode = 102
                    ElseIf NewAccTrans >= 80 And NewAccTrans <= 319 Then
                        GroupCode = 103
                    ElseIf NewAccTrans >= 320 Then
                        GroupCode = 104
                    End If
                    If GroupCode = 0 Then
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "',U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    Else
                        oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_DayPassBalB='" & NewBal & "',U_AI_ExpDate='" & Edt & "' , groupcode=" & GroupCode & ",U_AI_NoDayPassB=" & NewNotrans & ",U_AI_ACCDAYPASS=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                    End If

                End If

                oRecordSet6.MoveNext()
            Next
            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            oRecordSet6 = Nothing
            GC.Collect()
        Catch ex As Exception
            ' Functions.WriteLog(ex.Message)
            'MsgBox(ex.Message)
        End Try
    End Sub
    Public Function OwnerCode(ByVal PetCode As String, ByVal Ocompany As SAPbobsCOM.Company) As String
        Dim x As String = ""
        Dim oRecordSet_Ow As SAPbobsCOM.Recordset

        oRecordSet_Ow = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
        oRecordSet_Ow.DoQuery("SELECT T0.[U_AI_CustCode] FROM [dbo].[@AI_PETMASTER]  T0 WHERE T0.[U_PetCode] ='" & PetCode & "'")
        x = oRecordSet_Ow.Fields.Item(0).Value
        Return x
    End Function
#End Region
#Region "Boarding"
    Public Sub OpeningBalance_Bor()
        Try
            Dim PrimKey As String = BPCodeRe
            'If TextBox1.Text <> "" Then
            '    PrimKey = TextBox1.Text
            'End If
            ''Dim csvFileName As String = "K9DP1.csv"
            'Dim connString As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" _
            '       & "C:\k9\" & ";Extended Properties=""Text;HDR=No;FMT=Delimited"""
            'Dim conn As New Odbc.OdbcConnection(connString)
            'Dim da2 As New Odbc.OdbcDataAdapter("SELECT * FROM [" & csvFileName & "]", conn)
            'Dim DtSet_Master As System.Data.DataSet
            'DtSet_Master = New System.Data.DataSet
            'da2.Fill(DtSet_Master)
            Dim oRecordsetOB As SAPbobsCOM.Recordset
            oRecordsetOB = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordsetOB.DoQuery("SELECT T0.[U_Petcode], T0.[U_CardCode], T0.[U_BORBAL] FROM [dbo].[@OPENINGBALANCE]  T0 where  T0.[U_CardCode] like '" & PrimKey & "'")
            Dim rowcount_Master As Integer = oRecordsetOB.RecordCount 'DtSet_Master.Tables(0).Rows.Count
            '------------
            Dim i As Integer
            Dim BPCode As String = ""
            Dim PetCode As String = ""
            Dim PetSize As String = ""
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet As SAPbobsCOM.Recordset
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            Dim OldNoBor As Integer = 0
            Dim OldNoBorUsed As Integer = 0
            Dim NewNoBorBal As Integer = 0
            Dim NewNoBor As Integer = 0

            Dim OldNoTrs As Integer = 0
            Dim OldNoTrsUsed As Integer = 0
            Dim NewNoTrsBal As Integer = 0
            Dim NewNoTrs As Integer = 0
            Dim str As String = ""
            'For i = 0 To rowcount_Master - 1
            '    BPCode = DtSet_Master.Tables(0).Rows(i).Item(3).ToString()
            '    If BPCode <> "" Then
            NewNoTrs = 0
            NewNoBor = 0
            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            str = "update ocrd set U_AI_NoBoard='" & NewNoBor & "',[U_AI_AccumBoard]='" & NewNoBor & "' where cardcode like '" & PrimKey & "' "
            oRecordSet3.DoQuery(str)
            '    End If
            'Next
            For i = 0 To rowcount_Master - 1
                BPCode = oRecordsetOB.Fields.Item(1).Value 'DtSet_Master.Tables(0).Rows(i).Item(3).ToString()
                If BPCode <> "" Then
                    oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    str = "Select U_AI_NoBoard,U_AI_NoTrans from ocrd where cardcode='" & BPCode & "' "
                    oRecordSet2.DoQuery(str)
                    ' NewNoTrs = (DtSet_Master.Tables(0).Rows(i).Item(7) + oRecordSet2.Fields.Item(1).Value)
                    NewNoBor = (oRecordsetOB.Fields.Item(2).Value + oRecordSet2.Fields.Item(0).Value)
                    oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    str = "update ocrd set U_AI_NoBoard='" & NewNoBor & "',[U_AI_AccumBoard]='" & NewNoBor & "' where cardcode='" & BPCode & "' "
                    oRecordSet3.DoQuery(str)
                End If
                oRecordsetOB.MoveNext()

            Next
            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            str = "update OCRD set U_AI_BoardBal=U_AI_NoBoard-U_AI_BoardUsed"
            oRecordSet3.DoQuery(str)
            'oRecordSet3 = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            'str = "update OCRD set U_AI_TransBal=U_AI_NoTrans-U_AI_TransUsed"
            'oRecordSet3.DoQuery(str)

        Catch ex As Exception
            ' Functions.WriteLog(ex.Message)
            MsgBox(ex.Message)
        End Try
    End Sub


    Public Sub Boarding_Invoice()
        'SELECT T0.[CardCode], sum(T1.[Quantity]) as 'Qty' FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='103' group by T0.[CardCode]
        Try
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            Dim str As String = ""
            Dim CardCode As String = ""
            Dim Qty As Integer = 0
            Dim Notrans As Integer = 0
            Dim AccTrans As Integer = 0
            Dim NewNotrans As Integer = 0
            Dim NewAccTrans As Integer = 0
            Dim Used As Integer = 0
            Dim NewBalance As Integer = 0
            Dim PetName As String = ""

            Dim PrimKey As String = BPCodeRe
            'If TextBox1.Text <> "" Then
            '    PrimKey = TextBox1.Text
            'End If

            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            str = "update OCRD set U_AI_BoardUsed=0 where cardcode like '" & PrimKey & "'"
            oRecordSet3.DoQuery(str)

            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            str = "SELECT T0.[CardCode], sum(T1.[Quantity]) as 'Qty' FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='103' and T0.[CardCode] like '" & PrimKey & "' group by T0.[CardCode]"
            oRecordSet3.DoQuery(str)
            oRecordSet3.MoveFirst()
            Dim i As Integer = 0
            Dim j As Integer = 0
            For i = 0 To oRecordSet3.RecordCount - 1
                CardCode = oRecordSet3.Fields.Item(0).Value
                Qty = oRecordSet3.Fields.Item(1).Value
                oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet1.DoQuery("SELECT isnull(T0.[U_AI_NoBoard],0), isnull(T0.[U_AI_AccumBoard],0),isnull(T0.[U_AI_BoardUsed],0)  FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                Notrans = oRecordSet1.Fields.Item(0).Value
                NewNotrans = Qty + Notrans
                AccTrans = oRecordSet1.Fields.Item(1).Value
                NewAccTrans = Qty + AccTrans
                Used = oRecordSet1.Fields.Item(2).Value
                NewBalance = NewNotrans - Used
                oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_BoardBal='" & NewBalance & "',U_AI_NoBoard=" & NewNotrans & ",U_AI_AccumBoard=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                oRecordSet3.MoveNext()
            Next
        Catch ex As Exception
            ' Functions.WriteLog(ex.Message)
            'MsgBox(ex.Message)
        End Try
    End Sub
    Public Sub Boarding_CN()
        Try
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            Dim str As String = ""
            Dim CardCode As String = ""
            Dim Qty As Integer = 0
            Dim Notrans As Integer = 0
            Dim AccTrans As Integer = 0
            Dim NewNotrans As Integer = 0
            Dim NewAccTrans As Integer = 0
            Dim Used As Integer = 0
            Dim NewBalance As Integer = 0
            Dim PetName As String = ""
            Dim PrimKey As String = BPCodeRe
            'If TextBox1.Text <> "" Then
            '    PrimKey = TextBox1.Text
            'End If
            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            str = "SELECT T0.[CardCode], sum(T1.[Quantity]) as 'Qty' FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='103' and T0.[CardCode] like '" & PrimKey & "' group by T0.[CardCode]"
            oRecordSet3.DoQuery(str)
            oRecordSet3.MoveFirst()
            Dim i As Integer = 0
            Dim j As Integer = 0
            For i = 0 To oRecordSet3.RecordCount - 1
                CardCode = oRecordSet3.Fields.Item(0).Value
                Qty = oRecordSet3.Fields.Item(1).Value
                oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet1.DoQuery("SELECT T0.[U_AI_NoBoard], T0.[U_AI_AccumBoard], T0.[U_AI_BoardUsed]  FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                Notrans = oRecordSet1.Fields.Item(0).Value
                NewNotrans = Notrans - Qty
                AccTrans = oRecordSet1.Fields.Item(1).Value
                NewAccTrans = AccTrans - Qty
                Used = oRecordSet1.Fields.Item(2).Value
                NewBalance = NewNotrans - Used
                oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_BoardBal='" & NewBalance & "',U_AI_NoBoard=" & NewNotrans & ",U_AI_AccumBoard=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                oRecordSet3.MoveNext()
            Next
        Catch ex As Exception
            ' Functions.WriteLog(ex.Message)
            'MsgBox(ex.Message)
        End Try
    End Sub
    Public Sub Boarding_Attendance()
        Try
            Dim PrimKey As String = BPCodeRe
            'If TextBox1.Text <> "" Then
            '    PrimKey = TextBox1.Text
            'End If
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            Dim str As String = ""
            Dim CardCode As String = ""
            Dim Qty As Integer = 0
            Dim Notrans As Integer = 0
            Dim AccTrans As Integer = 0
            Dim NewNotrans As Integer = 0
            Dim NewAccTrans As Integer = 0
            Dim Used As Integer = 0
            Dim NewBalance As Integer = 0
            Dim PetName As String = ""
            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            str = "SELECT T1.[U_AI_CustCode], count(*) as Total FROM [dbo].[@AI_NIGHTSHIFT]  T0 left join [dbo].[@AI_PETMASTER]  T1 on   T1.[U_PetCode] = T0.[U_AI_PetCode] WHERE T0.[U_AI_PetCode]  <> '' and T1.[U_AI_CustCode] like '" & PrimKey & "' and isnull( T0.[U_Ignore],'')  <> 'Y' GROUP BY T1.[U_AI_CustCode]"
            oRecordSet3.DoQuery(str)
            oRecordSet3.MoveFirst()
            Dim i As Integer = 0
            Dim j As Integer = 0
            For i = 0 To oRecordSet3.RecordCount - 1
                CardCode = oRecordSet3.Fields.Item(0).Value
                Used = oRecordSet3.Fields.Item(1).Value
                oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet1.DoQuery("SELECT isnull(T0.[U_AI_NoBoard],0), isnull(T0.[U_AI_AccumBoard],0),isnull(T0.[U_AI_BoardUsed],0)  FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                Notrans = oRecordSet1.Fields.Item(0).Value
                NewNotrans = Qty + Notrans
                AccTrans = oRecordSet1.Fields.Item(1).Value
                NewAccTrans = Qty + AccTrans
                NewBalance = NewNotrans - Used
                oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet2.DoQuery("UPDATE OCRD SET [U_AI_BoardUsed]='" & Used & "',U_AI_AccumBoardU='" & Used & "',U_AI_BoardBal='" & NewBalance & "',U_AI_NoBoard=" & NewNotrans & ",U_AI_AccumBoard=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                oRecordSet3.MoveNext()
            Next
        Catch ex As Exception
            ' Functions.WriteLog(ex.Message)
            'MsgBox(ex.Message)
        End Try
    End Sub
#End Region
#Region "Transport"
    Public Sub OpeningBalance_Trs()
        Try
            'Dim csvFileName As String = "K9DP1.csv"
            'Dim connString As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" _
            '       & "C:\k9\" & ";Extended Properties=""Text;HDR=No;FMT=Delimited"""
            'Dim conn As New Odbc.OdbcConnection(connString)
            'Dim da2 As New Odbc.OdbcDataAdapter("SELECT * FROM [" & csvFileName & "]", conn)
            'Dim DtSet_Master As System.Data.DataSet
            'DtSet_Master = New System.Data.DataSet
            'da2.Fill(DtSet_Master)
            Dim PrimKey As String = BPCodeRe
            'If TextBox1.Text <> "" Then
            '    PrimKey = TextBox1.Text
            'End If
            Dim oRecordsetOB As SAPbobsCOM.Recordset
            oRecordsetOB = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordsetOB.DoQuery("SELECT T0.[U_Petcode], T0.[U_CardCode], T0.[U_TRNSBAL] FROM [dbo].[@OPENINGBALANCE]  T0 where T0.[U_CardCode] like '" & PrimKey & "'")
            Dim rowcount_Master As Integer = oRecordsetOB.RecordCount
            'Dim rowcount_Master As Integer = DtSet_Master.Tables(0).Rows.Count
            '------------
            Dim i As Integer
            Dim BPCode As String = ""
            Dim PetCode As String = ""
            Dim PetSize As String = ""
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet As SAPbobsCOM.Recordset
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            Dim OldNoBor As Integer = 0
            Dim OldNoBorUsed As Integer = 0
            Dim NewNoBorBal As Integer = 0
            Dim NewNoBor As Integer = 0

            Dim OldNoTrs As Integer = 0
            Dim OldNoTrsUsed As Integer = 0
            Dim NewNoTrsBal As Integer = 0
            Dim NewNoTrs As Integer = 0
            Dim str As String = ""
            'For i = 0 To rowcount_Master - 1
            '    BPCode = DtSet_Master.Tables(0).Rows(i).Item(3).ToString()
            '    If BPCode <> "" Then
            NewNoTrs = 0
            NewNoBor = 0
            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            str = "update ocrd set U_AI_NoTrans='" & NewNoTrs & "',[U_AI_AccumTrans]='" & NewNoTrs & "' where cardcode like '" & PrimKey & "'"
            oRecordSet3.DoQuery(str)
            '    End If
            'Next
            For i = 0 To rowcount_Master - 1
                BPCode = oRecordsetOB.Fields.Item(1).Value
                If BPCode <> "" Then
                    oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    str = "Select U_AI_NoBoard,U_AI_NoTrans from ocrd where cardcode='" & BPCode & "' "
                    oRecordSet2.DoQuery(str)
                    NewNoTrs = (oRecordsetOB.Fields.Item(2).Value + oRecordSet2.Fields.Item(1).Value)
                    ' NewNoBor = (DtSet_Master.Tables(0).Rows(i).Item(6) + oRecordSet2.Fields.Item(0).Value)
                    oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                    str = "update ocrd set U_AI_NoTrans='" & NewNoTrs & "',[U_AI_AccumTrans]='" & NewNoTrs & "' where cardcode='" & BPCode & "' "
                    oRecordSet3.DoQuery(str)
                End If
                oRecordsetOB.MoveNext()
            Next
            'oRecordSet3 = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            'str = "update OCRD set U_AI_BoardBal=U_AI_NoBoard-U_AI_BoardUsed"
            'oRecordSet3.DoQuery(str)
            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            str = "update OCRD set U_AI_TransBal=U_AI_NoTrans-U_AI_TransUsed"
            oRecordSet3.DoQuery(str)

        Catch ex As Exception
            ' Functions.WriteLog(ex.Message)
            ' MsgBox(ex.Message)
        End Try
    End Sub
    Public Sub Transport_Invoice()
        Try
            Dim PrimKey As String = BPCodeRe
            'If TextBox1.Text <> "" Then
            '    PrimKey = TextBox1.Text
            'End If

            Dim oRecordSet As SAPbobsCOM.Recordset
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            Dim st As String
            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            st = "update OCRD set  U_AI_TransUsed=0 where cardcode like '" & PrimKey & "'"
            oRecordSet3.DoQuery(st)

            oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.[CardCode], Sum(T1.[Quantity]) FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='110' and T0.[CardCode] like '" & PrimKey & "' group by T0.[CardCode]")
            Dim CardCode As String = ""
            Dim i As Integer
            Dim Qty As Integer = 0
            Dim Notrans As Integer = 0
            Dim AccTrans As Integer = 0
            Dim NewNotrans As Integer = 0
            Dim NewAccTrans As Integer = 0
            Dim Used As Integer = 0
            Dim NewBalance As Integer = 0
            oRecordSet.MoveFirst()
            Dim Edt As String = Format(Now.Date.AddMonths(6), "yyyyMMdd")
            'T0.[U_AI_BoardExp]
            For i = 1 To oRecordSet.RecordCount
                CardCode = oRecordSet.Fields.Item(0).Value
                Qty = oRecordSet.Fields.Item(1).Value
                oRecordSet1.DoQuery("SELECT T0.[U_AI_NoTrans], T0.[U_AI_AccumTrans],T0.[U_AI_TransUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                Notrans = oRecordSet1.Fields.Item(0).Value
                NewNotrans = Notrans + (Qty)
                AccTrans = oRecordSet1.Fields.Item(1).Value
                NewAccTrans = AccTrans + (Qty)
                Used = oRecordSet1.Fields.Item(2).Value
                NewBalance = NewNotrans - Used
                oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_TransBal=" & NewBalance & ",U_AI_NoTrans=" & NewNotrans & ",U_AI_AccumTrans=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                oRecordSet.MoveNext()
            Next
            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            GC.Collect()
            oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet3.DoQuery("SELECT T0.[CardCode], Sum(T1.[Quantity]) FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='109' and T0.[CardCode] like '" & PrimKey & "' group by T0.[CardCode]")
            oRecordSet3.MoveFirst()
            For i = 1 To oRecordSet3.RecordCount
                CardCode = oRecordSet3.Fields.Item(0).Value
                Qty = oRecordSet3.Fields.Item(1).Value
                oRecordSet1.DoQuery("SELECT T0.[U_AI_NoTrans], T0.[U_AI_AccumTrans],T0.[U_AI_TransUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                Notrans = oRecordSet1.Fields.Item(0).Value
                NewNotrans = Notrans + (Qty * 2)
                AccTrans = oRecordSet1.Fields.Item(1).Value
                NewAccTrans = AccTrans + (Qty * 2)
                Used = oRecordSet1.Fields.Item(2).Value
                NewBalance = NewNotrans - Used
                oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_TransBal=" & NewBalance & ",U_AI_NoTrans=" & NewNotrans & ",U_AI_AccumTrans=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                oRecordSet3.MoveNext()
            Next
            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            GC.Collect()
        Catch ex As Exception
            ' Functions.WriteLog(ex.Message)
            'MsgBox(ex.Message)
        End Try
    End Sub
    Public Sub Transport_CN()
        Try
            Dim PrimKey As String = BPCodeRe
            'If TextBox1.Text <> "" Then
            '    PrimKey = TextBox1.Text
            'End If
            Dim oRecordSet As SAPbobsCOM.Recordset
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.[CardCode], Sum(T1.[Quantity]) FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='110' and T0.[CardCode] like '" & PrimKey & "' group by T0.[CardCode]")
            Dim CardCode As String = ""
            Dim i As Integer
            Dim Qty As Integer = 0
            Dim Notrans As Integer = 0
            Dim AccTrans As Integer = 0
            Dim NewNotrans As Integer = 0
            Dim NewAccTrans As Integer = 0
            Dim Used As Integer = 0
            Dim NewBalance As Integer = 0
            oRecordSet.MoveFirst()
            Dim Edt As String = Format(Now.Date.AddMonths(6), "yyyyMMdd")
            'T0.[U_AI_BoardExp]
            For i = 1 To oRecordSet.RecordCount
                CardCode = oRecordSet.Fields.Item(0).Value
                Qty = oRecordSet.Fields.Item(1).Value
                oRecordSet1.DoQuery("SELECT T0.[U_AI_NoTrans], T0.[U_AI_AccumTrans],T0.[U_AI_TransUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                Notrans = oRecordSet1.Fields.Item(0).Value
                NewNotrans = Notrans - (Qty)
                AccTrans = oRecordSet1.Fields.Item(1).Value
                NewAccTrans = AccTrans - (Qty)
                Used = oRecordSet1.Fields.Item(2).Value
                NewBalance = NewNotrans - Used
                oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_TransBal=" & NewBalance & ",U_AI_NoTrans=" & NewNotrans & ",U_AI_AccumTrans=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                oRecordSet.MoveNext()
            Next
            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            GC.Collect()
            oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet3.DoQuery("SELECT T0.[CardCode], Sum(T1.[Quantity]) FROM ORIN T0  INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry WHERE (select T2.ItmsGrpCod from oitm T2 where T2.[ItemCode]= T1.[ItemCode])='109' and T0.[CardCode] like '" & PrimKey & "' group by T0.[CardCode]")
            oRecordSet3.MoveFirst()
            For i = 1 To oRecordSet3.RecordCount
                CardCode = oRecordSet3.Fields.Item(0).Value
                Qty = oRecordSet3.Fields.Item(1).Value
                oRecordSet1.DoQuery("SELECT T0.[U_AI_NoTrans], T0.[U_AI_AccumTrans],T0.[U_AI_TransUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                Notrans = oRecordSet1.Fields.Item(0).Value
                NewNotrans = Notrans - (Qty * 2)
                AccTrans = oRecordSet1.Fields.Item(1).Value
                NewAccTrans = AccTrans - (Qty * 2)
                Used = oRecordSet1.Fields.Item(2).Value
                NewBalance = NewNotrans - Used
                oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_TransBal=" & NewBalance & ",U_AI_NoTrans=" & NewNotrans & ",U_AI_AccumTrans=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                oRecordSet3.MoveNext()
            Next
            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            GC.Collect()
        Catch ex As Exception
            ' Functions.WriteLog(ex.Message)
            'MsgBox(ex.Message)
        End Try
    End Sub
    Public Sub Transport_Attendance()
        Try
            Dim PrimKey As String = BPCodeRe
            'If TextBox1.Text <> "" Then
            '    PrimKey = TextBox1.Text
            'End If

            Dim oRecordSet As SAPbobsCOM.Recordset
            Dim oRecordSet1 As SAPbobsCOM.Recordset
            Dim oRecordSet2 As SAPbobsCOM.Recordset
            Dim oRecordSet3 As SAPbobsCOM.Recordset
            oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT distinct T0.[U_AI_Date], T1.[U_AI_CustCode],count(*) as 'Co',1 as 'Tot' FROM [dbo].[@AI_ATTENDANCE]  T0 left join [dbo].[@AI_PETMASTER]  T1 on  T1.[U_PetCode] = T0.[U_AI_PetCode] WHERE T1.[U_PetCode] <> '' and   T0.[U_AI_Pickup] ='Y' and T1.[U_AI_CustCode] like '" & PrimKey & "' GROUP BY T0.[U_AI_Date], T1.[U_AI_CustCode]")
            Dim CardCode As String = ""
            Dim i As Integer
            Dim Qty As Integer = 0
            Dim Notrans As Integer = 0
            Dim AccTrans As Integer = 0
            Dim NewNotrans As Integer = 0
            Dim NewAccTrans As Integer = 0
            Dim Used As Integer = 0
            Dim UsedOld As Integer = 0
            Dim NewBalance As Integer = 0
            oRecordSet.MoveFirst()
            'Dim Edt As String = Format(Now.Date.AddMonths(6), "yyyyMMdd")
            'T0.[U_AI_BoardExp]
            For i = 1 To oRecordSet.RecordCount
                CardCode = oRecordSet.Fields.Item(1).Value
                NewBalance = 0
                oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_TransUsed=" & NewBalance & ",U_AI_AccumTransU=" & NewBalance & " where CardCode='" & CardCode & "'")
                oRecordSet.MoveNext()
            Next
            ' MsgBox("Hiiiiiii")
            oRecordSet.MoveFirst()
            For i = 1 To oRecordSet.RecordCount
                Used = 0
                UsedOld = 0
                Notrans = 0
                NewNotrans = 0
                AccTrans = 0
                NewBalance = 0
                Qty = 0
                CardCode = oRecordSet.Fields.Item(1).Value
                oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet1.DoQuery("SELECT T0.[U_AI_NoTrans], T0.[U_AI_AccumTrans],T0.[U_AI_TransUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                Notrans = oRecordSet1.Fields.Item(0).Value
                NewNotrans = Notrans + (Qty)
                AccTrans = oRecordSet1.Fields.Item(1).Value
                NewAccTrans = AccTrans + (Qty)
                UsedOld = oRecordSet1.Fields.Item(2).Value
                Used = oRecordSet.Fields.Item(3).Value
                NewBalance = NewNotrans - (Used + UsedOld)
                oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_TransUsed=" & (Used + UsedOld) & ",U_AI_AccumTransU=" & (Used + UsedOld) & " ,U_AI_TransBal=" & NewBalance & ",U_AI_NoTrans=" & NewNotrans & ",U_AI_AccumTrans=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                oRecordSet.MoveNext()
            Next
            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            GC.Collect()
            oRecordSet3 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet3.DoQuery("SELECT distinct T0.[U_AI_Date], T1.[U_AI_CustCode],count(*),1 as 'Tot' FROM [dbo].[@AI_ATTENDANCE]  T0 left join [dbo].[@AI_PETMASTER]  T1 on  T1.[U_PetCode] = T0.[U_AI_PetCode] WHERE T1.[U_PetCode] <> '' and   T0.[U_AI_Dropoff]='Y' and T1.[U_AI_CustCode] like '" & PrimKey & "' GROUP BY T0.[U_AI_Date], T1.[U_AI_CustCode]")
            oRecordSet3.MoveFirst()
            For i = 1 To oRecordSet3.RecordCount
                Used = 0
                UsedOld = 0
                Notrans = 0
                NewNotrans = 0
                AccTrans = 0
                NewBalance = 0
                oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
                CardCode = oRecordSet3.Fields.Item(1).Value
                Qty = 0
                oRecordSet1.DoQuery("SELECT T0.[U_AI_NoTrans], T0.[U_AI_AccumTrans],T0.[U_AI_TransUsed] FROM OCRD T0 WHERE T0.[CardCode] ='" & CardCode & "'")
                Notrans = oRecordSet1.Fields.Item(0).Value
                NewNotrans = Notrans + (Qty * 2)
                AccTrans = oRecordSet1.Fields.Item(1).Value
                NewAccTrans = AccTrans + (Qty * 2)
                UsedOld = oRecordSet1.Fields.Item(2).Value
                Used = oRecordSet3.Fields.Item(3).Value
                NewBalance = NewNotrans - (Used + UsedOld)
                oRecordSet2.DoQuery("UPDATE OCRD SET U_AI_TransUsed=" & (Used + UsedOld) & ",U_AI_AccumTransU=" & (Used + UsedOld) & " ,U_AI_TransBal=" & NewBalance & ",U_AI_NoTrans=" & NewNotrans & ",U_AI_AccumTrans=" & NewAccTrans & " where CardCode='" & CardCode & "'")
                oRecordSet3.MoveNext()
            Next
            oRecordSet = Nothing
            oRecordSet1 = Nothing
            oRecordSet2 = Nothing
            GC.Collect()
        Catch ex As Exception
            ' Functions.WriteLog(ex.Message)
            'MsgBox(ex.Message)
        End Try
    End Sub
#End Region


    Public Function DisplayStatus(ByVal oFrmParent As SAPbouiCOM.Form, ByVal sMsg As String, ByRef sErrDesc As String) As Long
        ' ***********************************************************************************
        '   Function   :    DisplayStatus()
        '   Purpose    :    Display Status Message while loading 
        '
        '   Parameters :    ByVal oFrmParent As SAPbouiCOM.Form
        '                       oFrmParent = set the SAP UI Form Object
        '                   ByVal sMsg As String
        '                       sMsg = set the Display Message information
        '                   ByRef sErrDesc As String
        '                       sErrDesc = Error Description to be returned to calling function
        '   Return     :    0 - FAILURE
        '                   1 - SUCCESS
        '   Author     :    Sri
        '   Date       :   29 April 2013
        '   Change     :
        ' ***********************************************************************************
        Dim oForm As SAPbouiCOM.Form
        Dim oItem As SAPbouiCOM.Item
        Dim oTxt As SAPbouiCOM.StaticText
        Dim creationPackage As SAPbouiCOM.FormCreationParams
        Dim iCount As Integer
        Dim sFuncName As String = String.Empty

        Try
            sFuncName = "DisplayStatus"
            'If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting function", sFuncName)
            'Check whether the form exists.If exists then close the form
            For iCount = 0 To SBO_Application.Forms.Count - 1
                oForm = SBO_Application.Forms.Item(iCount)
                If oForm.UniqueID = "dStatus" Then
                    oForm.Close()
                    Exit For
                End If
            Next iCount
            'Add Form
            ' If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Creating form Assign Department", sFuncName)
            creationPackage = SBO_Application.CreateObject(SAPbouiCOM.BoCreatableObjectType.cot_FormCreationParams)
            creationPackage.UniqueID = "dStatus"
            creationPackage.BorderStyle = SAPbouiCOM.BoFormBorderStyle.fbs_FixedNoTitle
            creationPackage.FormType = "AB_dStatus"
            oForm = SBO_Application.Forms.AddEx(creationPackage)
            With oForm
                .AutoManaged = False
                .Width = 300
                .Height = 100
                If oFrmParent Is Nothing Then
                    .Left = (Screen.PrimaryScreen.WorkingArea.Width - oForm.Width) / 2
                    .Top = (Screen.PrimaryScreen.WorkingArea.Height - oForm.Height) / 2.5
                Else
                    .Left = ((oFrmParent.Left * 2) + oFrmParent.Width - oForm.Width) / 2
                    .Top = ((oFrmParent.Top * 2) + oFrmParent.Height - oForm.Height) / 2
                End If
            End With

            'Add Label
            oItem = oForm.Items.Add("3", SAPbouiCOM.BoFormItemTypes.it_STATIC)
            oItem.Top = 40
            oItem.Left = 40
            oItem.Width = 250
            oTxt = oItem.Specific
            oTxt.Caption = sMsg
            oForm.Visible = True

            'DisplayStatus = RTN_SUCCESS
            'If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS", sFuncName)
        Catch exc As Exception
            'DisplayStatus = RTN_ERROR
            'sErrDesc = exc.Message
            'Call WriteToLogFile(sErrDesc, sFuncName)
            'If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
        Finally
            creationPackage = Nothing
            oForm = Nothing
            oItem = Nothing
            oTxt = Nothing
        End Try

    End Function

End Class
